<template>
  <div class="articleList">
    <van-pull-refresh
      :success-text="tipText"
      v-model="refreshing"
      @refresh="onRefresh"
      success-duration="1500"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!-- <van-cell v-for="item in list" :key="item.art_id" :title="item.title" /> -->
        <article-item
          v-for="item in list"
          :key="item.art_id + ''"
          :row="item"
        ></article-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticleList } from '@/api/article.js'
import ArticleItem from '@/components/ArticleItem'

export default {
  components: { ArticleItem },
  props: {
    channelsId: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: Date.now(),
      error: false,
      refreshing: false,
      tipText: ''
    }
  },

  created () {},

  methods: {
    async onLoad () {
      try {
        const res = await getArticleList({
          channel_id: this.channelsId,
          timestamp: this.timestamp,
          with_top: 1
        })
        if (Math.random() > 0.5) {
          console.lg(12)
        }
        this.list.push(...res.results)
        this.timestamp = res.pre_timestamp
        this.loading = false
        if (!res.results.length) {
          this.finished = true
        }
      } catch (error) {
        this.error = true
        this.loading = false
      }
      // setTimeout(() => {
      //   for (let i = 0; i < 10; i++) {
      //     this.list.push(this.list.length + 1)
      //   }
      // 加载状态结束
      // this.loading = false
      // 数据全部加载完成
      //   if (this.list.length >= 40) {
      //     this.finished = true
      //   }
      // }, 1000)
    },
    async onRefresh () {
      try {
        const res = await getArticleList({
          channel_id: this.channelsId,
          timestamp: Date.now(),
          with_top: 1
        })
        if (Math.random() > 0.5) {
          console.lg(12)
        }
        // console.log(res)
        this.refreshing = false
        this.tipText = '刷新成功！'
        this.list.unshift(...res.results)
      } catch (error) {
        console.log(error)
        this.refreshing = false
        this.tipText = '刷新失败！请稍后重试！'
      }
    }
  }
}
</script>

<style scoped lang="less">
.articleList {
  height: 80vh;
  overflow-y: scroll;
}
</style>
